package top.lyunk.demo.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * <pre>
 * 说明：
 * </pre>
 *
 * @author lyk
 * @date 2022/9/19
 */
public class CuratorDemo {
    public static void main(String[] args) throws Exception {
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .connectionTimeoutMs(5000)
                .sessionTimeoutMs(60 * 1000)
                // 重连3次，每次休息3秒
                .retryPolicy(new RetryNTimes(3, 3000))
                // 重连3次，预估等待1秒，最长等待2秒
                // .retryPolicy(new ExponentialBackoffRetry(1000, 3, 2000))
                // 默认命名空间，会自动创建
                .namespace("curator")
                .build();
        client.start();

        Stat stat = client.checkExists().forPath("/t1");
        System.out.println(stat);
        String path = null;
        if (stat == null) {
            path = client.create().forPath("/t1", "ctx".getBytes());
        }
        System.out.println(path);
        path = client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/t2");
        System.out.println(path);

        Thread.sleep(Integer.MAX_VALUE);
    }
}
